﻿using Aliyun.OSS;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zhao.Common.AliyunUtil.Interface;
using Zhao.Common.AliyunUtil.Model;
using Zhao.Common.EnumEntity;

namespace Zhao.Common.AliyunUtil.Impl
{
    /// <summary>
    /// 阿里云OSS操作类
    /// </summary>
    public class AliyunOssClient : IOssClient
    {
        private IOss _ossClient;
        private readonly AliyunOSSSetting _aliyunOSSSetting;

        public AliyunOssClient(IOptionsSnapshot<AliyunOSSSetting> options)
        {
            _aliyunOSSSetting = options.Value;
            _ossClient = new OssClient(
                endpoint: _aliyunOSSSetting.AliyunEndpoint,
                accessKeyId: _aliyunOSSSetting.AliyunAccessKeyId,
                accessKeySecret: _aliyunOSSSetting.AliyunAccessKeySecret
            );
        }

        /// <summary>
        /// 文件上传
        /// </summary>
        /// <param name="ossFileType">上传文件类型</param>
        /// <param name="FileName">文件名s</param>
        /// <param name="FilePath">本地文件路径</param>
        public string Upload(OssFileType ossFileType, string FileName, string FilePath)
        {
            var DateNow = DateTime.Now;
            var Year = DateNow.Year.ToString("00");
            var Month = DateNow.Month.ToString("00");
            string UploadFilePath = $"/{ossFileType.ToString().ToLower()}/{Year}/{Month}/{FileName.ToLower()}";//文件储存路径
            //长度必须在 1–1023 字节之间。不能以正斜线（/）或者反斜线（\）开头。
            var ObjectKey = FilePath.TrimStart('/', '\\');
            _ossClient.PutObject(_aliyunOSSSetting.AliyunBucketName, ObjectKey, FilePath);
            return UploadFilePath;
        }

        /// <summary>
        /// 文件上传（用流的方式）
        /// </summary>
        /// <param name="ossFileType">上传文件类型</param>
        /// <param name="FileName">文件名</param>
        /// <param name="inputStream">存储流对象</param>
        public string Upload(OssFileType ossFileType, string FileName, Stream inputStream)
        {
            var DateNow = DateTime.Now;
            var Year = DateNow.Year.ToString("00");
            var Month = DateNow.Month.ToString("00");
            string FilePath = $"/{ossFileType.ToString().ToLower()}/{Year}/{Month}/{FileName.ToLower()}";//文件储存路径
            //长度必须在 1–1023 字节之间。不能以正斜线（/）或者反斜线（\）开头。
            var ObjectKey = FilePath.TrimStart('/', '\\');
            _ossClient.PutObject(_aliyunOSSSetting.AliyunBucketName, ObjectKey, inputStream);
            return FilePath;
        }
        public string Upload(string FileName, Stream inputStream)
        {
            var ObjectKey = FileName.ToLower().TrimStart('/', '\\');
            _ossClient.PutObject(_aliyunOSSSetting.AliyunBucketName, ObjectKey, inputStream);
            return FileName;
        }

        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="ObjectKey"></param>
        public DeleteObjectResult Delete(string ObjectKey, bool delete = false)
        {
            //长度必须在 1–1023 字节之间。不能以正斜线（/）或者反斜线（\）开头。
            ObjectKey = ObjectKey.TrimStart('/', '\\');
            if (delete)
            {
                return _ossClient.DeleteObject(_aliyunOSSSetting.AliyunBucketName, ObjectKey);
            }
            return null;
        }

        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="Url"></param>
        /// <returns></returns>
        public AliyunOssObjModel Download(string Url)
        {
            //长度必须在 1–1023 字节之间。不能以正斜线（/）或者反斜线（\）开头。
            var ObjectKey = Url.TrimStart('/', '\\');
            var OssObj = _ossClient.GetObject(_aliyunOSSSetting.AliyunBucketName, ObjectKey);
            return new AliyunOssObjModel
            {
                FileName = Path.GetFileName(Url),
                FileStream = OssObj.Content,
                FileLength = OssObj.ContentLength
            };
        }

    }
}
